package com.hjxr.quota.define.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.hjxr.common.core.controller.BaseController;
import com.hjxr.common.utils.StringUtils;
import com.hjxr.quota.define.domain.QuotaValue;
import com.hjxr.quota.define.service.IQuotaDimRelationDetailService;
import com.hjxr.quota.define.service.QuotaValueService;
import com.hjxr.quota.util.PageUtil;
import com.hjxr.quota.datasource.controller.BaseForm;
import com.hjxr.quota.datasource.domain.QuotaDimension;
import com.hjxr.quota.datasource.service.IQuotaDimensionDetailService;
import com.hjxr.quota.datasource.service.IQuotaDimensionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 指标值表控制器层
 *
 * @author sun
 * @version v1.0
 * @since 2020-10-09
 */
@RestController
@RequestMapping("/quotaValue")
@Api(tags = "指标值接口")
public class QuotaValueController extends BaseController {

    @Autowired
    private QuotaValueService quotaValueService;
    @Autowired
    IQuotaDimensionDetailService quotaDimensionDetailService;
    @Autowired
    IQuotaDimensionService quotaDimensionService;
    @Autowired
    IQuotaDimRelationDetailService quotaDimRelationDetailService;

    @PostMapping("/list")
    @ApiOperation("查询指标")
    public R<PageUtil> list(@RequestBody Map<String, Object> params) {
        if (null == params.get("quotaId")) {
            return failed("请先选择指标");
        }
        return success(quotaValueService.selectList(params));
    }

    @GetMapping
    @ApiOperation("分页查询所有数据")
    @ApiImplicitParams(
            {@ApiImplicitParam(paramType = "query", dataType = "String", name = "current", value = "当前页", defaultValue
                    = "1", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "Boolean", name = "ifCount", value = "是否查询总数",
                            defaultValue = "true"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "ascs", value = "升序字段，多个用逗号分隔"),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "descs", value = "降序字段，多个用逗号分隔")
            })
    public R<IPage<QuotaValue>> selectAll() {
        BaseForm form = new BaseForm();
        QueryWrapper<QuotaValue> query = (QueryWrapper<QuotaValue>) form.pageQueryWrapperCustom(form.getParameters(),
                new QueryWrapper<QuotaValue>());
        IPage<QuotaValue> page = quotaValueService.page(form.getPlusPagingQueryEntity(), query);
        for (QuotaValue value : page.getRecords()) {
            String dimNames = "";
            if (null != value.getDimensions() && !"".equals(value.getDimensions())) {
                String[] dims = value.getDimensions().split(",");
                for (int i = 0; i < dims.length; i++) {
                    if (!"".equals(dims[i])) {
                        Integer id = Integer.parseInt(dims[i]);
                        QuotaDimension dimension = quotaDimensionService.getById(id);
                        dimNames += dimension.getDimensionName();
                    }
                    if (i < dims.length - 1)
                        dimNames += ",";
                }
                value.setDimensionNames(dimNames);
            }
        }

        return success(page);
    }

    @ApiOperation(value = "注入指标值")
    @GetMapping("/addQuotaValue")
    public R<Boolean> addQuotaValue(@RequestParam Integer tablenameAliasId) {
        try {
            quotaValueService.addQuotaValue(tablenameAliasId);
        } catch (Exception e) {
            return failed("注入时发生错误");
        }
        return success(true);
    }

    /**
     * 指标值表操作 更新
     */
    @PostMapping("/updateValue")
    @ApiOperation(value = "更新指标值")
    public R<Boolean> updateValue(@RequestBody QuotaValue quotaValue) {
        return success(quotaValueService.updateValue(quotaValue));
    }

    @PostMapping("/getReportJson")
    @ApiOperation(value = "获取指标报表json数据")
    public R<String> getReportJson(@RequestBody Map<String, Object> param) {
        Map<String, String> quota = (Map<String, String>) param.get("quota");
        List<Integer> dimensionIds = (List<Integer>) param.get("dimension");
        String type = (String) param.get("type");
        if (StringUtils.isBlank(type)) {
            type = "0";
        }
        if (quota == null || quota.size() <= 0) {
            return failed("请选择指标");
        }
        if (dimensionIds == null || dimensionIds.size() <= 0) {
            return failed("请选择维度");
        }
        return success(quotaValueService.getReportJson(quota, dimensionIds, type));
    }

    @PostMapping("/getBarChartsJson")
    @ApiOperation(value = "获取指标柱状图json数据")
    public R<String> getBarChartsJson(@RequestBody Map<String, Object> param) {
        Map<String, String> quota = (Map<String, String>) param.get("quota");
        List<Integer> dimensionIds = (List<Integer>) param.get("dimension");
        Integer limit = (Integer) param.get("limit");
        String type = (String) param.get("type");
        if (StringUtils.isBlank(type)) {
            type = "0";
        }
        if (quota == null || quota.size() <= 0) {
            return failed("请选择指标");
        }
        if (dimensionIds == null || dimensionIds.size() <= 0) {
            return failed("请选择维度");
        }
        return success(quotaValueService.getBarChartsJson(quota, dimensionIds, type, limit == null ? 0 : limit));
    }

    @PostMapping("/getPieChartsJson")
    @ApiOperation(value = "获取指标饼图json数据")
    public R<String> getPieChartsJson(@RequestBody Map<String, Object> params) {
        Map<String, String> quota = (Map<String, String>) params.get("quota");
        List<Integer> dimensionIds = (List<Integer>) params.get("dimension");
        String type = (String) params.get("type");
        Integer limit = (Integer) params.get("limit");
        if (StringUtils.isBlank(type)) {
            type = "0";
        }
        if (quota == null || quota.size() <= 0) {
            return failed("请选择指标");
        }
        if (dimensionIds == null || dimensionIds.size() <= 0) {
            return failed("请选择维度");
        }
        return success(quotaValueService.getPieChartsJson(quota, dimensionIds, type, limit == null ? 0 : limit));
    }

    @PostMapping("/getScatterChartsJson")
    @ApiOperation(value = "获取指标散点图json数据")
    public R<String> getScatterChartsJson(@RequestBody Map<String, Object> params) {
        Map<String, String> quota = (Map<String, String>) params.get("quota");
        List<Integer> dimensionIds = (List<Integer>) params.get("dimension");
        String type = (String) params.get("type");
        Integer limit = (Integer) params.get("limit");
        if (StringUtils.isBlank(type)) {
            type = "0";
        }
        if (quota == null || quota.size() <= 0) {
            return failed("请选择指标");
        }
        if (dimensionIds == null || dimensionIds.size() <= 0) {
            return failed("请选择维度");
        }
        return success(quotaValueService.getScatterChartsJson(quota, dimensionIds, type, limit == null ? 0 : limit));
    }
}
